/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package utils;

import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 * Utility class for managing an image that consists of pixel indices and a
 * color palette.
 * 
 * <p>
 * A pixel index {@code p} is transparent if {@code p <= TRANSPARENT_INDEX}.
 * 
 * @author luke
 */
public class PalettizedImage {
	public static final short TRANSPARENT_INDEX = -1;
	public static final short MAX_OPACITY = 255;
	public static final short MIN_OPACITY = 0;
	private int width;
	private int height;
	private byte[] palette;
	private short[] pixelIndices;

	/**
	 * PalettizedImage constructor.
	 * 
	 * @param width the image width.
	 * @param height the image height.
	 * @param palette the color palette as an RGB byte array.
	 * @param pixelIndices the indices of each pixel in the palette.
	 */
	public PalettizedImage(int width, int height, byte[] palette, short[] pixelIndices) {
		this.width = width;
		this.height = height;
		this.palette = palette;
		this.pixelIndices = pixelIndices;
	}

	/**
	 * PalettizedImage constructor with no pixel indices.
	 * 
	 * @see #PalettizedImage(int, int, byte[], short[])
	 */
	public PalettizedImage(int width, int height, byte[] palette) {
		this(width, height, palette, null);
	}

	/**
	 * PalettizedImage constructor with no arguments.
	 * 
	 * @see #PalettizedImage(int, int, byte[], short[])
	 */
	public PalettizedImage() {
		this(0, 0, null, null);
	}

	/**
	 * Creates and returns an Image from the pixel indices and palette.
	 * 
	 * @return an Image for this PalettizedImage.
	 */
	public Image createImage() {
		int pixelCount = this.width * this.height;
		int[] iArray = new int[pixelCount * 4];
		for (int i = 0; i < pixelCount; i++) {
			boolean transparent = this.pixelIndices[i] <= TRANSPARENT_INDEX;
			for (int j = 0; j < 3; j++) {
				iArray[4 * i + j] = transparent ? 0 : this.palette[3 * this.pixelIndices[i] + j];
			}
			iArray[4 * i + 3] = transparent ? MIN_OPACITY : MAX_OPACITY;
		}
		BufferedImage bufferedImage = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
		bufferedImage.getRaster().setPixels(0, 0, this.width, this.height, iArray);
		return bufferedImage;
	}

	public int getWidth() {
		return this.width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return this.height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setPalette(byte[] palette) {
		this.palette = palette;
	}

	public void setPixelIndices(short[] pixelIndices) {
		this.pixelIndices = pixelIndices;
	}
}
